from tortoise import fields
from app.models.base_model import BaseModel
from app.core.constants import FileStatusEnum, StorageProviderEnum


class FileModel(BaseModel):
    """文件模型"""

    filename = fields.CharField(max_length=255, description="原始文件名")
    storage_path = fields.CharField(max_length=500, null=True, description="存储路径")
    provider = fields.ForeignKeyField("models.StorageProviderModel", related_name="files", description="存储供应商")
    size = fields.BigIntField(description="文件大小(字节)")
    content_type = fields.CharField(max_length=100, description="文件类型")
    md5 = fields.CharField(max_length=32, description="文件MD5")
    status = fields.IntEnumField(
        FileStatusEnum, default=FileStatusEnum.PENDING, description="文件状态: 1=待处理, 2=上传中, 3=已完成, 4=失败"
    )

    # 上传者信息
    uploader = fields.ForeignKeyField("models.UserModel", related_name="uploaded_files", description="上传者")

    class Meta:
        table = "sys_file"
        indexes = (
            ("filename",),  # 文件名索引
            ("md5",),  # MD5索引
            ("status",),  # 状态索引
            ("created_at",),  # 创建时间索引
            ("uploader_id",),  # 上传者索引
        )


class StorageProviderModel(BaseModel):
    """存储供应商模型"""

    name = fields.CharField(max_length=50, description="供应商名称")
    provider_type = fields.IntEnumField(
        StorageProviderEnum, description="供应商类型: 1=本地存储, 2=阿里云OSS, 3=AWS S3, 4=腾讯云COS"
    )
    access_key = fields.CharField(max_length=100, null=True, description="AccessKey")
    secret_key = fields.CharField(max_length=100, null=True, description="SecretKey")
    endpoint = fields.CharField(max_length=200, null=True, description="访问端点")
    bucket = fields.CharField(max_length=100, null=True, description="存储桶名称")
    region = fields.CharField(max_length=50, null=True, description="区域")
    base_url = fields.CharField(max_length=200, description="基础访问URL")
    is_default = fields.BooleanField(default=False, description="是否为默认供应商")
    is_active = fields.BooleanField(default=True, description="是否启用")

    class Meta:
        table = "sys_storage_provider"
